package com.czn.interceptor;

import com.czn.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ChenZeNan
 * @create 2021-04-25 18:00
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) { // 没有登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/home/loginRegisterPage");
            return false;
        }
        return true;
    }
}

